Skip to content

Compiler-As-3

Question 1

Use the grammar rules and attribute definitions in Lecture 10 Page 19 to construct the parse tree and syntax tree for expression "(3+1)(4b)" assuming the expression is tokenized as "(num+num)(numid)" (20pt)

Parse Tree:

Tree

Syntax Tree: Syntax Tree

Question 2

Use the grammar rules and attribute definitions in “Grammar+Typing.docx” (the grammar is in Lec 12 on pg 20) to analyze the source code

python
bool flip(bool x) not x; flip (flase)

Parse Tree

Symbol Table

NameType
flipboolbool
x (Removed)bool

Question 3

The language in Lec 11 on Page 27 has two production rules:

1. T1T22. T1T2[num]

So, it is possible to have valid variable declarations

c
int *[10]x; int[10]* y;

Please follow the corresponding type checking rules on the same page to give the type of x and y. You need to explain your answer. (15pt)

The type of x is array(09,pointer(int.type)) The type of y is pointer(array(0,9,int.type))

Question 4

The language in Lec 11 on Page 27 is too weak. It cannot make assignment from a memory location to a pointer. Thus, we power-up the language by modifying the grammar rules. (New or modified rules are in red color.) Define the correct type checking rules for the new grammar rules. Type checking for old grammar rules remain unchanged. 20pt

  1. PD; S; E
  2. D1D2;D3
  3. DT id
  4. S1S2;S3
  5. SI=R
  6. Iid
  7. Iid[E]
  8. R&id
  9. RE
  10. T1char
  11. T1int
  12. T1T2 
  13. T1T2[num]
  14. Echar_lit
  15. Enum
  16. Eid
  17. E1E2 mod E3
  18. E1E2
  19. E1E2[E3]

RE R.type:=E.type

R&id
if(id.is_variable==true)

then R.type:=pointer(look_up(id.entry)))else R.type:=type_error

Iid[E]
if (E.type==int.type and look_up(id.entry)==array(i,t) and E.val<iand E.val>=0)

then I.type:=t else I.type:=type_error

Iid I.type:=look_up(id.entry)

S1S2;S3 no need to define type SI=R no need to define type PD; S; E no need to define type

Question 5

Let the following grammar rules be the syntax of integer arithmetic.

  1. EE+T
  2. ET
  3. TT×F
  4. TF
  5. F(E)
  6. Fnum

Remark:

  • Token "num" is an integer constant.
  • "+" and "×" are integer addition and multiplication respectively. Multiplication has the higher precedence. Furthermore, both operators are (left and right) associative.

However, this language is not perfect. It may contain many redundant parentheses.

For example, ((2 × 3) + 4) can be entirely without the parentheses as 2 × 3 + 4. (You can try to parse them.) Thus, please design semantics for the grammar rules to remove redundant parentheses. (25 pt)

Hint:

  • The semantics should have at least two attributes.
  • One attribute is "expression", which holds the expression for each substructure.
  • After computing the semantics, E.expression has the final outcome — the arithmetic expression without useless parentheses.
  • The value of attribute "expression" is treated as a string. And the operator "" can be used (in semantic rules) to concatenate two strings.

Define the semantics for the grammar rules.

  1. expression: Stores the simplified expression without redundant parentheses.
  2. priority: Stores the priority of the operator in the expression.

denotes the concatenation of two strings.

Define a function for checking whether parentheses are needed for a given expression.

python
function checkParen(childExpr, childPriority, parentPriority):
    if childPriority < parentPriority then
        return "(" + childExpr + ")"
    else
        return childExpr
  1. EE1+T

    • E.priority=1
    • E.expression=E1.expression+T.expression
  2. ET

    • E.priority=T.priority
    • E.expression=T.expression
  3. TT1×F

    • T.priority=2
    • T.expression=checkParen(T1.expression,T1.priority,T.priority)×checkParen(F.expression,F.priority,T.priority)
  4. TF

    • T.priority=F.priority
    • T.expression=F.expression
  5. F(E)

    • F.priority=E.priority
    • F.expression=E.expression
  6. Fnum

    • F.priority=3
    • F.expression=num